package vaccine.appointment.system.appointmentrecord.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vaccine.appointment.system.appointmentrecord.service.AppointmentRecordService;
import vaccine.appointment.system.appointmentrecord.vo.AppointmentRecordVO;
import vaccine.appointment.system.appointmentrecord.vo.CreateAppointmentRequest;
import vaccine.appointment.system.common.annotation.AutoWrapResult;
import vaccine.appointment.system.common.annotation.RequireRole;
import vaccine.appointment.system.user.enums.IdentityInfoEnum;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 预约记录控制器
 */
@AutoWrapResult
@RequestMapping("/appointment")
@RestController
@RequireRole(IdentityInfoEnum.NORMAL_USER)
public class AppointmentRecordController {

    @Autowired
    private AppointmentRecordService appointmentRecordService;

    /**
     * 创建预约
     * @param request 创建预约请求
     * @param httpRequest HTTP请求
     * @return 是否成功
     */
    @PostMapping
    public Boolean createAppointment(@Valid @RequestBody CreateAppointmentRequest request, HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return appointmentRecordService.createAppointment(userId, request);
    }

    /**
     * 查询预约记录列表
     * @param status 预约状态（可选）
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param httpRequest HTTP请求
     * @return 分页数据
     */
    @GetMapping("/list")
    public Page<AppointmentRecordVO> getAppointmentList(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return appointmentRecordService.getAppointmentList(userId, status, pageNum, pageSize);
    }

    /**
     * 取消预约
     * @param id 预约ID
     * @param httpRequest HTTP请求
     * @return 是否成功
     */
    @PutMapping("/{id}/cancel")
    public Boolean cancelAppointment(@PathVariable Integer id, HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return appointmentRecordService.cancelAppointment(userId, id);
    }

    /**
     * 查询逾期未接种的预约记录
     * @param httpRequest HTTP请求
     * @return 逾期预约列表
     */
    @GetMapping("/overdue")
    public List<AppointmentRecordVO> getOverdueAppointments(HttpServletRequest httpRequest) {
        Integer userId = (Integer) httpRequest.getAttribute("userId");
        return appointmentRecordService.getOverdueAppointments(userId);
    }
}
